using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.IO;

namespace TraceHelper
{
	/// <summary>
	/// Descrizione di riepilogo per Form1.
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.OpenFileDialog openFileDialog1;
		private System.Windows.Forms.TextBox txtTraceFile;
		private System.Windows.Forms.Button btnSelect;
		private System.Windows.Forms.TreeView treeTracer;
		/// <summary>
		/// Variabile di progettazione necessaria.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public MainForm()
		{
			//
			// Necessario per il supporto di Progettazione Windows Form
			//
			InitializeComponent();

			//
			// TODO: aggiungere il codice del costruttore dopo la chiamata a InitializeComponent
			//
		}

		/// <summary>
		/// Pulire le risorse in uso.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Codice generato da Progettazione Windows Form
		/// <summary>
		/// Metodo necessario per il supporto della finestra di progettazione. Non modificare
		/// il contenuto del metodo con l'editor di codice.
		/// </summary>
		private void InitializeComponent()
		{
			this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
			this.txtTraceFile = new System.Windows.Forms.TextBox();
			this.btnSelect = new System.Windows.Forms.Button();
			this.treeTracer = new System.Windows.Forms.TreeView();
			this.SuspendLayout();
			// 
			// txtTraceFile
			// 
			this.txtTraceFile.Location = new System.Drawing.Point(8, 16);
			this.txtTraceFile.Name = "txtTraceFile";
			this.txtTraceFile.Size = new System.Drawing.Size(744, 22);
			this.txtTraceFile.TabIndex = 0;
			this.txtTraceFile.Text = "";
			// 
			// btnSelect
			// 
			this.btnSelect.Location = new System.Drawing.Point(760, 16);
			this.btnSelect.Name = "btnSelect";
			this.btnSelect.Size = new System.Drawing.Size(120, 23);
			this.btnSelect.TabIndex = 1;
			this.btnSelect.Text = "Select Trace File...";
			this.btnSelect.Click += new System.EventHandler(this.btnSelect_Click);
			// 
			// treeTracer
			// 
			this.treeTracer.ImageIndex = -1;
			this.treeTracer.Location = new System.Drawing.Point(8, 56);
			this.treeTracer.Name = "treeTracer";
			this.treeTracer.SelectedImageIndex = -1;
			this.treeTracer.Size = new System.Drawing.Size(1176, 608);
			this.treeTracer.TabIndex = 2;
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
			this.ClientSize = new System.Drawing.Size(1200, 672);
			this.Controls.Add(this.treeTracer);
			this.Controls.Add(this.btnSelect);
			this.Controls.Add(this.txtTraceFile);
			this.Name = "MainForm";
			this.Text = "Form1";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// Il punto di ingresso principale dell'applicazione.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			WAPI.Response response = new WAPI.Response();
			WAPI.Session.InitSession(ref response, "TraceHelper", 7);

			if ( response.iCode != 0 )
			{
				MessageBox.Show ("Impossible to start TraceHelper. Scenario is not initialized.");
				return;
			}

			Application.Run(new MainForm());

			WAPI.Session.EndSession(ref response);
		}

		private void btnSelect_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog openFileDialog1 = new OpenFileDialog();

			openFileDialog1.Filter = "Scenario Trace Files files (*.trace)|*.trace";
			openFileDialog1.FilterIndex = 1 ;
			openFileDialog1.RestoreDirectory = true ;

			if(openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				this.treeTracer.Text = openFileDialog1.FileName;
				this.OpenTraceFile (this.treeTracer.Text);
			}
		}

		private void OpenTraceFile(string sFileName)
		{
			TreeNode mainNode = treeTracer.Nodes.Add (sFileName);
			using (StreamReader sr = File.OpenText(sFileName)) 
			{
				string s = string.Empty;
				int iLevel=0;
				TreeNode currentNode = mainNode;
				while ((s = sr.ReadLine()) != null) 
				{
					//blank space
					int pos = s.IndexOf ("-");

					if ( pos != -1)
					{
						pos = s.IndexOf ("-", pos+1);

						int i=pos+1;
						bool bNotEnd=true;
						while ( (i<s.Length) && bNotEnd)
						{
							if ( s[i] != ' ' )
							{
								bNotEnd = false;
							}
							else
							{
								i++;
							}
						}

						for ( int j=(i-pos-1);j<iLevel;j++)
						{
							currentNode = currentNode.Parent;
						}

						currentNode = currentNode.Nodes.Add (s);

						iLevel = i-pos;

						/*
						if ( s.IndexOf ("-OUT[") > -1)
						{
							mainNode.Nodes.Add (s);
						}
						*/
					}
				}
			}
		}
	}
}
